package com.yuanheng100.ems4j.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanheng100.ems4j.api.internal.IInsertable;
import com.yuanheng100.ems4j.api.internal.ISelectable;
import com.yuanheng100.ems4j.api.internal.IUpdateable;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * Created by j4love on 2016/12/14.
 * Description : 数据操作的抽象类,包含对记录的增，改，查操作
 */
public abstract class AbstractDataOperationService<T> implements IInsertable<T> ,IUpdateable<T>,ISelectable<T> {

    public static Logger logger = Logger.getLogger(AbstractDataOperationService.class);
    @Autowired
    private Mapper<T> mapper;

    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public int insert(T entity) {
        return mapper.insert(entity);
    }

    @Override
    public T selectByPrimaryKey(T entity) {
        return mapper.selectByPrimaryKey(entity);
    }

    @Override
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    @Override
    public int updateByPrimaryKey(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateBySelective(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 分页查询数据
     * @param pageNum 当前页码
     * @param pageSize 每页显示的记录数
     * @param entity
     * @return
     */
    public PageInfo selectPage(int pageNum,int pageSize,T entity){
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<T>(selectList(entity));
    }
}
